//
//  NetworkLoadView.m
//  JiHe
//
//  Created by liuguochao on 16/3/29.
//  Copyright © 2016年 JiHe. All rights reserved.
//

#import "NetworkLoadView.h"

@implementation NetworkLoadView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        self.alpha = 1;
    }
    return self;
};

- (void)netLoadIngType:(NetWorkLoadType)netWorkLoadType
     andImageAnimation:(NSArray *)imageAnimationArray
         andPromptText:(NSString *)promptText
{   self.hidden = NO;
    self.alpha = 1;
    //相同类型 直接返回
    if (self.netWorkLoadType  == netWorkLoadType) {
        return;
    }else{
        [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [(UIView*)obj removeFromSuperview];
        }];
    }
    switch (netWorkLoadType) {
        case NetWorkLoadTypeDefault: {
            self.indicatorView.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
            [self addSubview:self.indicatorView];
            
            if (!promptText.length) {
                promptText = @"加载中";
            }
            self.promptLabel.text = promptText;
            [self.promptLabel sizeToFit];
            self.promptLabel.center = CGPointMake(self.indicatorView.center.x,CGRectGetMaxY(self.indicatorView.frame) + 10 );
            [self addSubview:self.promptLabel];
        }break;
        case NetWorkLoadTypeImageAnimation: {
            if (!imageAnimationArray.count) {
                return;
            }
            self.imageView.frame = CGRectMake(0, 0, ((UIImage*)[imageAnimationArray firstObject]).size.width, ((UIImage*)[imageAnimationArray firstObject]).size.height);
            self.imageView.animationImages = imageAnimationArray;
            self.imageView.center =  CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
            [self addSubview:self.imageView];
        } break;
        case NetWorkLoadTypeImageAnimationAndPromptText: {
            if (!imageAnimationArray.count) {
                return;
            }
            self.imageView.frame = CGRectMake(0, 0, ((UIImage*)[imageAnimationArray firstObject]).size.width, ((UIImage*)[imageAnimationArray firstObject]).size.height);
            self.imageView.animationImages = imageAnimationArray;
            self.imageView.center =  CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
            [self addSubview:self.imageView];
            if (!promptText.length) {
                promptText = @"加载中";
            }
            self.promptLabel.text = promptText;
            self.promptLabel.textAlignment = NSTextAlignmentCenter;
            [self.promptLabel sizeToFit];
            self.promptLabel.center = CGPointMake(self.imageView.center.x, CGRectGetMaxY(self.imageView.frame) + 10);
            [self addSubview:self.promptLabel];
        } break;
        default: {
            
        }break;
    }
}
/**
 *  懒加载网络加载UI
 *
 *  @return
 */
- (UIActivityIndicatorView *)indicatorView
{
    if (!_indicatorView) {
        self.indicatorView = ({
            UIActivityIndicatorView  *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            indicatorView;
        });
    }
    return _indicatorView;
}
/**
 *  懒加载
 *
 *  @return 文字提示label
 */
- (UILabel *)promptLabel
{
    if (!_promptLabel) {
        self.promptLabel = ({
            UILabel *lable = [[UILabel alloc]init];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.textColor = [UIColor redColor];
            lable.font = [UIFont boldSystemFontOfSize:14];
            lable;
        });
    }
    return _promptLabel;
}
/**
 *  懒加载
 *
 *  @return 产生动画的imageView
 */
- (UIImageView *)imageView
{
    if (!_imageView) {
        self.imageView = ({
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.animationRepeatCount = 0;
            imageView.animationDuration = 1;
            imageView;
        });
    }
    return _imageView;
}

/**
 *  可用CG 画图
 *  @param rect
 */
- (void)drawRect:(CGRect)rect
{
}
@end

